package com.yanqu.road.server.manger.config;


import com.yanqu.road.entity.config.beauty.BeautyInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.wineshop.WineShopBuilding;
import com.yanqu.road.entity.config.wineshop.WineShopEvent;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.wineshop.UserWineShopBuilding;
import com.yanqu.road.entity.wineshop.UserWineShopData;
import com.yanqu.road.logic.bussiness.config.WineShopBusiness;
import com.yanqu.road.logic.bussiness.player.UserBeautyBussiness;
import com.yanqu.road.logic.bussiness.player.UserWineShopBusiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.wineshop.WineShopProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.wineshop.WineShopModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.WineShopPb;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @author ：zfr
 * @date ：2021/6/30 16:23
 */
public class WineShopMgr extends TempMgr {
    private static Map<Integer, Map<Integer, WineShopBuilding>> regionIdBuildingMap = new ConcurrentHashMap<>(); //region - id - info
    private static Map<Integer, WineShopBuilding> wineShopBuildingMap = new ConcurrentHashMap<>(); //id - info
    private static Map<Integer, WineShopEvent> wineShopEventMap = new ConcurrentHashMap<>();
    public static int WINESHOP_MAX_NICK_NAME_LENGTH = 12;
    public static int WINESHOP_MIN_NICK_NAME_LENGTH = 6;
    private static List<Integer> regionSort = new ArrayList<>();

    public static WineShopBuilding getWineShopBuilding(int id) {
        return wineShopBuildingMap.get(id);
    }

    public static Map<Integer, WineShopEvent> getWineShopEventMap() {
        return wineShopEventMap;
    }

    public static Map<Integer, WineShopBuilding> getRegionBuildingList(int region) {
        return regionIdBuildingMap.get(region);
    }

    public static List<WineShopBuilding> getLastRegionBuildingList(int region) {
        String defaultUnlockZoneParam = GameConfig.WINESHOP_DEFAUT_UNLOCK_ZONE;
        String[] split = defaultUnlockZoneParam.split(";");
        int lastIndex = 0;
        boolean isValid = false;
        for (int i = split.length; i < regionSort.size(); i++) {
            if (regionSort.get(i) == region) {
                lastIndex = i - 1;
                isValid = true;
                break;
            }
        }
        if (!isValid) {
            throw new RuntimeException("WINE SHOP BUILDING SORT CONFIG IS ERROR !!!, region:" + region);
        }
        List<WineShopBuilding> wineShopBuildings = new ArrayList<>();
        for (WineShopBuilding wineShopBuilding : wineShopBuildingMap.values()) {
            if (wineShopBuilding.getRegion() == regionSort.get(lastIndex)) {
                wineShopBuildings.add(wineShopBuilding);
            }
        }
        return wineShopBuildings;
    }

    public static int getWineShopSkillAddValue(GamePlayer player, int skillType, int occupation) {
        int addValue = 0;
        Map<Integer, UserWineShopBuilding> userWineShopBuildingMap = player.getModule(WineShopModule.class).getUserWineShopBuildingMap();
        for (UserWineShopBuilding building : userWineShopBuildingMap.values()) {
            WineShopBuilding buildingInfo = getWineShopBuilding(building.getId());
            if (buildingInfo != null) {
                SkillInfo skillInfo = SkillMgr.getSkillInfo(Integer.parseInt(buildingInfo.getSkillId()));
                if (skillInfo != null) {
                    if (skillInfo.getType() == skillType && (skillInfo.getParamList().get(2) == 0 || skillInfo.getParamList().get(2) == occupation)) {
                        addValue += SkillMgr.getSkillAddition(skillInfo, building.getLv());
                    }
                }
            }
        }
        return addValue;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        WineShopBusiness.parseWineShopBuildingMap(regionIdBuildingMap, wineShopBuildingMap);
        parseEventMap();
        parseRegionSort();
        return true;
    }

    private void parseRegionSort() {
        regionSort = new ArrayList<>();
        String defaultUnlockZoneParam = GameConfig.WINESHOP_DEFAUT_UNLOCK_ZONE;
        String[] split = defaultUnlockZoneParam.split(";");
        for (String regionStr : split) {
            if (!regionSort.contains(Integer.parseInt(regionStr))) {
                regionSort.add(Integer.parseInt(regionStr));
            }
        }
        List<WineShopBuilding> sortBuildingList = new ArrayList<>(wineShopBuildingMap.values());
        sortBuildingList.sort(Comparator.comparing(WineShopBuilding::getSort));
        for (WineShopBuilding shopBuilding : sortBuildingList) {
            if (!regionSort.contains(shopBuilding.getRegion())) {
                regionSort.add(shopBuilding.getRegion());
            }
        }
    }

    public static List<Integer> getRegionSort() {
        return regionSort;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    private void parseEventMap() {
        String wineShopBeautyEventConfig = GameConfig.WINESHOP_BEAUTY_EVENT_CONFIG;
        String[] split = wineShopBeautyEventConfig.split("\\|");
        int id = 1;
        for (String s : split) {
            String[] split1 = s.split(";");
            WineShopEvent event = new WineShopEvent();
            event.setId(id);
            event.setCateringTimes(Integer.parseInt(split1[0]));
            event.setEntertainmentTimes(Integer.parseInt(split1[1]));
            event.setWeight(Integer.parseInt(split1[2]));
            wineShopEventMap.put(event.getId(), event);
            id++;
        }
    }

    /**
     * 获取改名消耗
     * @param changeNameTimes 改名次数
     * @return cost
     */
    public static BigInteger getChangeNickNameCost(int changeNameTimes) {
        int cost = 0;
        if (changeNameTimes > 0) {
            String[] split = GameConfig.WINESHOP_CHANGE_NICKNAME_COST.split(";");
            cost = Integer.parseInt(split[0]) + ((changeNameTimes - 1) * Integer.parseInt(split[1]));
            if (cost > Integer.parseInt(split[2])) {
                cost = Integer.parseInt(split[2]);
            }
        }
        return BigInteger.valueOf(cost);
    }

    public static WineShopProto.VisitOtherWineShopRespMsg.Builder visitWineShop(long otherPlayerId) {
        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(otherPlayerId);
        UserWineShopData userWineShopData;
        Map<Integer, UserWineShopBuilding> userWineShopBuildingMap;
        List<UserBeauty> beautyList;
        UserInfo userInfo = UserMgr.getUserInfo(otherPlayerId);
        if (onlinePlayer != null) {
            userWineShopData = onlinePlayer.getModule(WineShopModule.class).getUserWineShopData();
            userWineShopBuildingMap = onlinePlayer.getModule(WineShopModule.class).getUserWineShopBuildingMap();
            beautyList = new ArrayList<>(onlinePlayer.getModule(BeautyModule.class).getUserBeautyMap().values());
        } else {
            //读库
            userWineShopData = UserWineShopBusiness.getUserWineShopData(otherPlayerId);
            userWineShopBuildingMap = UserWineShopBusiness.getUserWineShopBuildingMap(otherPlayerId);
            Map<Integer, UserBeauty> userBeautyMap = UserBeautyBussiness.getUserBeautyMap(otherPlayerId);
            try {
                //指定国内渠道屏蔽同时拥有美女103001和103209的,屏蔽103209
                int channelId = ServerListMgr.getChannelId(GameServer.getInstance().getServerId());
                if (
                        channelId == 201 ||
                        channelId == 221 ||
                        channelId == 231 ||
                        channelId == 241 ||
                        channelId == 301 ||
                        channelId == 311 ||
                        channelId == 321) {
                    if (userBeautyMap.containsKey(103001)){
                        userBeautyMap.remove(103209);
                    }
                }
            } catch (Exception e) {
                getLogger().error(e);
            }

            beautyList = new ArrayList<>(userBeautyMap.values());
        }
        return WineShopPb.parseVisitWineShopMsg(userWineShopData, userWineShopBuildingMap, userInfo, beautyList);
    }

    public static List<Integer> getItTakesTwoForwardRegions() {
        List<Integer> itTakesTwoForwardRegions = new ArrayList<>();
        String[] split = GameConfig.WINESHOP_IT_TAKES_TWO_FORWARD_REGIONS.split(";");
        for (String s : split) {
            itTakesTwoForwardRegions.add(Integer.parseInt(s));
        }
        return itTakesTwoForwardRegions;
    }

    /**
     * 计算单个客人银币收入
     * @param buildingMap
     * @return
     */
    public static long calcMoneyReward(Map<Integer, UserWineShopBuilding> buildingMap) {
        long moneyReward = 0;
        for (UserWineShopBuilding building : buildingMap.values()) {
            WineShopBuilding buildingInfo = getWineShopBuilding(building.getId());
            moneyReward += (buildingInfo.getBaseMoney() + buildingInfo.getUpgradeMoney() * (building.getLv() - 1));
        }
        return moneyReward;
    }

    /**
     * 是否是美女
     * @param beautyId
     * @return
     */
    public static boolean isBeauty(int beautyId) {
        BeautyInfo beautyInfo = BeautyMgr.getBeautyInfo(beautyId);
        return beautyInfo != null;
    }

    public static long calcExp(int wineShopLv, int skillType) {
        long exp = 0L;
        String skillStr = GameConfig.WINESHOP_MAIN_BUILDING_LEVEL_CONFIG.split("\\|")[1];
        String[] skillArr = skillStr.split(";");
        for (String skillIdStr : skillArr) {
            SkillInfo skillInfo = SkillMgr.getSkillInfo(Integer.parseInt(skillIdStr));
            if (skillInfo != null && skillInfo.getType() == skillType) {
                exp += SkillMgr.getSkillAddition(skillInfo, wineShopLv);
            }
        }
        return exp;
    }


    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }
}
